package com.mdd.front.service.impl;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.SystemMsg;
import com.mdd.common.mapper.SystemMsgMapper;
import com.mdd.common.util.ProjectConstant;
import com.mdd.common.util.RedisUtils;
import com.mdd.common.util.StringUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.front.service.IMsgService;
import com.mdd.front.validate.common.PageValidate;
import com.mdd.front.vo.msg.SystemMsgVo;

import cn.hutool.core.util.StrUtil;

@Service
public class MsgServiceImpl implements IMsgService{

	@Autowired
	private SystemMsgMapper systemMsgMapper;
	
	@Override
	public PageResult<SystemMsg> qryMsgList(Integer userId, Integer type, PageValidate pageValidate) {
		Integer pageNo = pageValidate.getPageNo();
		Integer pageSize = pageValidate.getPageSize();
		LambdaQueryWrapper<SystemMsg> lambdaQueryWrapper = new LambdaQueryWrapper<SystemMsg>();
		lambdaQueryWrapper.and(i ->i.eq(SystemMsg::getUserId, userId).or().eq(SystemMsg::getUserId, 0));
		lambdaQueryWrapper.orderByDesc(SystemMsg::getCreateTime);
		lambdaQueryWrapper.eq(SystemMsg::getType, type);
		Page<SystemMsg> iPage = systemMsgMapper.selectPage(new Page<SystemMsg>().setCurrent(pageNo).setSize(pageSize), lambdaQueryWrapper);
		iPage.getRecords().stream().forEach(msg ->{
			if(null != msg.getCreateTime()) {
				msg.setCreateTime(msg.getCreateTime()*1000);
			}
		});
		return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords());
	}
	
	@Override
	public AjaxResult<Object> sendExpressDeliveryMsg(SystemMsg msg) {
		msg.setCreateTime(TimeUtils.timestamp());
		msg.setType(1);
		systemMsgMapper.insert(msg);
		// 计入未读消息
		String key = ProjectConstant.REDIS_MSG_PREFIX+msg.getType()+StrUtil.COLON+msg.getUserId();
		RedisUtils.incr(key, 1);
		return AjaxResult.success();
	}

	public static void main(String[] args) {
		Long timestamp = TimeUtils.timestamp();
		
		System.out.println(TimeUtils.timestampToDate((timestamp),"yyyy-MM-dd HH:mm"));
	}
	@Override
	public AjaxResult<List<SystemMsgVo>> qryMsgFront(Integer userId) {
		String kuaidiMsg = "暂无消息";
		String kuaiShangMsg = "暂无消息";
		Integer kuaidiNum = 0;
		Integer kuaiStringNum = 0;
		String kuaidiTime = "";
		String kuaiShangTime = "";
		LambdaQueryWrapper<SystemMsg> lambdaQueryWrapper = new LambdaQueryWrapper<SystemMsg>();
		lambdaQueryWrapper.and(i ->i.eq(SystemMsg::getUserId, userId).or().eq(SystemMsg::getUserId, 0));
		lambdaQueryWrapper.orderByDesc(SystemMsg::getCreateTime);
		SystemMsg kuaidi = systemMsgMapper.selectOne(lambdaQueryWrapper.eq(SystemMsg::getType, 1).last("limit 1"));
		if(null != kuaidi && StringUtils.isNotEmpty(kuaidi.getTitle())) {
			kuaidiMsg = kuaidi.getTitle();
			if(null == kuaidi.getCreateTime()) {
				kuaidi.setCreateTime(TimeUtils.timestamp());
			}
			kuaidiTime = TimeUtils.timestampToDate((kuaidi.getCreateTime()),"yyyy-MM-dd HH:mm");
			
			Object value = RedisUtils.get(ProjectConstant.REDIS_MSG_PREFIX+kuaidi.getType()+StrUtil.COLON+userId);
			if(null != value) {
				kuaidiNum = Integer.valueOf(value.toString());
			}
		}
		SystemMsg kuaiShang = systemMsgMapper.selectOne(lambdaQueryWrapper.eq(SystemMsg::getType, 2).last("limit 1"));
		if(null != kuaiShang && StringUtils.isNotEmpty(kuaiShang.getTitle())) {
			kuaiShangMsg = kuaiShang.getTitle();
			if(null == kuaiShang.getCreateTime()) {
				kuaiShang.setCreateTime(TimeUtils.timestamp());
			}
			kuaiShangTime = TimeUtils.timestampToDate((kuaiShang.getCreateTime()),"yyyy-MM-dd HH:mm");
			Object value = RedisUtils.get(ProjectConstant.REDIS_MSG_PREFIX+kuaiShang.getType()+StrUtil.COLON+userId);
			if(null != value) {
				kuaiStringNum = Integer.valueOf(value.toString());
			}
		}
		List<SystemMsgVo> list = new ArrayList<SystemMsgVo>();
		for (int i = 1; i <=2; i++) {
			SystemMsgVo vo = new SystemMsgVo();
			vo.setType(i);
			vo.setUnreadNum(i==1?kuaidiNum:kuaiStringNum);
			vo.setMsgContent(i==1?kuaidiMsg:kuaiShangMsg);
			vo.setTime(i==1?kuaidiTime:kuaiShangTime);
			list.add(vo);
		}
		return AjaxResult.success(list);
	}

	@Override
	public AjaxResult<Object> cleanUnreadMsg(Integer userId, Integer type) {
		String key = ProjectConstant.REDIS_MSG_PREFIX+type+StrUtil.COLON+userId;
		RedisUtils.del(key);
		return AjaxResult.success();
	}

}
